package com.crazy.practice.boot.controller;

import cn.hutool.aop.ProxyUtil;
import com.crazy.exception.ValidationException;
import com.crazy.practice.boot.even.UserOrderEven;
import com.crazy.practice.boot.service.UserService;
import com.crazy.practice.boot.service.impl.UserServiceImpl;
import com.crazy.practice.model.AverageUser;
import com.crazy.practice.model.UserOrder;
import com.crazy.proxy.CglibProxy;
import com.crazy.proxy.MyAspect;
import com.crazy.proxy.JdkProxy;
import com.crazy.util.DateUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author panyonghui
 * @describe 用户控制器
 * @date 2022/1/17 9:37
 */
@Slf4j
@RestController
@RequestMapping(path = "/")
@RefreshScope
public class UserController {

    @Value("${ac.name:}")
    String userName;

    /**
     * 监听器事件发布器
     */
    @Autowired
    private ApplicationEventPublisher applicationEventPublisher;

    @RequestMapping(path = "/")
    public String defaultPath(){
        return "Welcome to browse! ";
    }

    /**
     * 跨域交易，允许https://www.baidu.com访问该交易
     * @param id
     * @return java.lang.String
     */
    @CrossOrigin(value = "https://www.baidu.com")
    @GetMapping("/getUserName")
    public String userNameQry(@RequestParam(value = "id") String id){
        return "Bob" + id;
    }

    @GetMapping("/testError")
    public void testError(){
        String name = null;
        name.indexOf("a");
    }

    @GetMapping("/testValidation")
    public void testValidation() throws ValidationException {
        throw new ValidationException("validation.test.error", new Object[]{"name", "sex11"});
    }

    @GetMapping("/accountInfoQry")
    public Map accountInfoQry(){
        Map dataMap = new HashMap<>();
        dataMap.put("acName", userName);
        dataMap.put("acNo", "62365200002233");
        return dataMap;
    }

    @GetMapping("/acListQry")
    public List acListQry(){
        List list = new ArrayList();
        Map dataMap = new HashMap<>();
        dataMap.put("acName", "户名");
        dataMap.put("acNo", "62365200002233");

        Map dataMap2 = new HashMap<>();
        dataMap2.put("acName", "黄龙");
        dataMap2.put("acNo", "62365200002256464");
        list.add(dataMap);
        list.add(dataMap2);
        return list;
    }

    @GetMapping("/ipAttribute")
    public Map ipAttribute(Model model){
        Map dataMap = new HashMap();
        dataMap.putAll(model.asMap());
        return dataMap;
    }

    @GetMapping("/userList")
    public Map userList(){
        Map dataMap = new HashMap();
        UserServiceImpl userServiceImpl = new UserServiceImpl();
        // 基于jdk的动态代理（代理类需要实现接口）
        UserService userService = JdkProxy.getProxyInstance(userServiceImpl, MyAspect.class);
        System.out.println("自定义代理：" + userService.userOrderQuery(null));
        dataMap.put("userList1", userService.userOrderQuery(null));

        // HuTool工具类的代理方法
        UserService userService1 = ProxyUtil.proxy(userServiceImpl, MyAspect.class);
        AverageUser averageUser = new AverageUser();
        averageUser.setUserState("N");
        averageUser.setUserMobilePhone("16620874134");
        averageUser.setCreateTime(new Date());
        System.out.println("HuTool代理：" + userService1.userOrderQuery(averageUser));
        dataMap.put("userList2", userService1.userOrderQuery(averageUser));

        // 基于spring的cglib动态代理
        UserService userServiceImpl3 = new UserServiceImpl();
        UserService userService3 = CglibProxy.getProxyInstance(userServiceImpl3, MyAspect.class);
        System.out.println(userService3.userOrderQuery(null));

        // 查询订单列表
        List<UserOrder> userOrderList = userService.userOrderQuery(averageUser);
        UserOrderEven userOrderEven = new UserOrderEven(userOrderList);
        // 发布自定义事件
        applicationEventPublisher.publishEvent(userOrderEven);
        return dataMap;
    }
}
